/**
 * 使用 rollup.watch 监听文件变化，进行持续构建
 * @author liaoJie
 * @since 2022-06-19 10:57:25
 */
import * as rollup from 'rollup';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';

const watcher = rollup.watch({
  input: './src/index.js',
  plugins: [resolve(), commonjs()],
  output: [
    {
      format: 'esm',
      dir: 'dist/es',
      entryFileNames: '[name].js',
      chunkFileNames: 'chunk-[name].js',
      sourcemap: true,
      assetFileNames: 'assets/[name]-[hash][extname]',
    },
    {
      format: 'commonjs',
      dir: 'dist/cjs',
    },
  ],
  watch: {
    include: ['src/**'],
  },
});

// 监听 watcher 的各种事件
watcher.on('restart', () => {
  console.log('重新构建...');
});

watcher.on('change', (id) => {
  console.log('发生变动的模块id: ', id);
});

watcher.on('event', (e) => {
  if (e.code === 'BUNDLE_END') {
    console.log('构建结束');
  }
});
